﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.Text;

using EmbeddedSensorCloud.HTTP;

namespace EmbeddedSensorCloud.Plugin.Navigation
{
    public sealed class Program : IPlugin
    {
        private enum RequestType
        {
            Error,
            Standard,
            Search,
            Reload
        }

        private readonly ResponseCreator _Worker;
        private readonly string PATH_TO_FILE;

        #region Constructor/Init

        public Program(string pathToOSM)
        {
            this.PATH_TO_FILE = pathToOSM;
            this._Worker = new ResponseCreator(PATH_TO_FILE);
        }

        public void Initialize()
        {
            this._Worker.ReloadXMLFile();
        }

        #endregion

        #region IPlugin Interface

        public bool IsResponseable(Request request)
        {
            return request.Url.StartsWith("/Navi", true, CultureInfo.InvariantCulture);
        }

        public Response Respond(Request request)
        {
            Response responseToClient = new Response();
            string responseHTML = Properties.Resources.ResourceManager.GetString("index");

            switch (CheckURL(request.Url, request.Args))
            {
                case RequestType.Standard:
                    responseHTML = responseHTML.Replace("@@data@@", "Enter a street to search for...");
                    break;
                case RequestType.Search:
                    string foundCities = this._Worker.SearchForStreet(request.Args["street"]);
                    
                    if (foundCities != "")
                    { responseHTML = responseHTML.Replace("@@data@@", foundCities); }
                    else
                    { responseHTML = responseHTML.Replace("@@data@@", "The street map is currently reloading. Pleast try again in a few minutes..."); }
                    break;
                case RequestType.Reload:
                    if (this._Worker.ReloadXMLFile())
                    { responseHTML = responseHTML.Replace("@@data@@", "The street map will now reload."); }
                    else
                    { responseHTML = responseHTML.Replace("@@data@@", "The street map is currently reloading. Pleast try again in a few minutes..."); }
                    break;
                default:
                    responseHTML = "<h1>Wrong Navi URL</h1>";
                    break;
            }
            responseToClient.Body = Encoding.UTF8.GetBytes(responseHTML);

            return responseToClient;
        }

        #endregion

        #region CheckURL

        private RequestType CheckURL(string url, Dictionary<string, string> args)
        {
            string[] urlParts = url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            switch (urlParts.Length)
            {
                case 1:
                    if (args.ContainsKey("street") && args["street"].Length > 0)
                    {
                        return RequestType.Search;
                    }
                    return RequestType.Standard;
                case 2:
                    if (urlParts[1].ToLowerInvariant() == "reload")
                    {
                        return RequestType.Reload;
                    }
                    break;
            }
            return RequestType.Error;
        }

        #endregion
    }
}
